package com.tmeone.erpone.controller;


import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TDepartment;
import com.tmeone.erpone.entity.TPost;
import com.tmeone.erpone.mapper.TDepartmentMapper;
import com.tmeone.erpone.service.ITDepartmentService;
import com.tmeone.erpone.service.ITPostService;
import org.beetl.ext.fn.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-19
 */
@RestController
public class TDepartmentController {
    @Autowired
    private ITDepartmentService departmentService;

    @Autowired
    private ITPostService postService;

    //查询所有的部门
    @GetMapping("departments")
    public JsonResult selectDepartment(){
        return new JsonResult(1,departmentService.selectDepartment());
    }
    //查询一个
    @GetMapping("/departmentOne")
    public JsonResult selectOne(String id){
        TDepartment tDepartment = departmentService.selectOneDepartment(id);
        return new JsonResult(1,tDepartment);
    }

    //查询所有的部门职位
    @GetMapping("tPost")
    public JsonResult selectAll(){
        return new JsonResult(1,postService.list());
    }

    //删除部门
    @DeleteMapping("department/{id}")
    public JsonResult deleteDepartment(@PathVariable String id){
        return new JsonResult(1,departmentService.removeById(id));
    }

    //修改部门
    @PutMapping("department")
    public JsonResult updateDepartment(@RequestBody TDepartment department){
        String id = UUID.randomUUID().toString().replace("-","").substring(0,8);
        String name= department.getfPoName();
        String did = department.getId();
        TPost tPost = new TPost(id,name,did);
        postService.save(tPost);
        departmentService.updateById(department);
        return new JsonResult(1,null);
    }

    //添加部门
    @PostMapping("/department")
    public JsonResult addDepartment(@RequestBody TDepartment department){
        department.setId(UUID.randomUUID().toString().replace("-","").substring(0,8));
        return new JsonResult(1,departmentService.save(department));
    }
}
